package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.home.BR
import com.stars.home.config.ConfigRx
import com.stars.home.model.User
import com.stars.home.utils.net.NetEngine
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import kotlinx.coroutines.launch
import java.util.ArrayList

class PersonalInfoVModel(application: Application) : BViewModel<BRepository>(application) {

    val user: MutableLiveData<User> = MutableLiveData()
    var headImg: MutableLiveData<String> = MutableLiveData()

    var nopath: ArrayList<String> = ArrayList() //全路径

    var typeChose=MutableLiveData<Int>()//1默认头像 2图片
    var workTime:String?=null
    var sex:String?=null
    var address:String?=null





    @Bindable
    open var name: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }

    fun choseImg() {


        typeChose.value=1


    }

    fun choseMoreImg(){
        //选择图片

        typeChose.value=2

    }
    fun clearMoreImg(position: Int) {
        nopath.removeAt(position)
    }


    fun getUserInfo(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.getUser() }
            if(it.ok()){
                user.value=it.data

                headImg.value=it.data?.headImage
                name=it.data?.nickname


            }else{
            }
        }
    }


    fun toSubmit(images:String?){

        viewModelScope.launch {
           var it= repository().lreq { NetEngine.service.memberEdit(headImg.value,name,images,sex,workTime,address) }
            if(it.ok(true)){
                RxBus.get().post(ConfigRx.REFRESH_USER, "")
                (repository().context as Activity).finish()
            }
        }


    }




}



